package teleradiologia.web;

import java.util.Date;
import java.util.HashMap;

import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import teleradiologia.domain.Diagnosys;
import teleradiologia.domain.Doctor;
import teleradiologia.domain.Ordering;
import teleradiologia.domain.Person;
import teleradiologia.service.DiagnosysManipulator;
import teleradiologia.service.DoctorManipulator;
import teleradiologia.service.OrderingManipulator;
import teleradiologia.service.PersonManipulator;

@Controller
public class DoctorController {
	private OrderingManipulator orderingManipulator;
	private PersonManipulator personManipulator;
	private DoctorManipulator doctorManipulator;
	private DiagnosysManipulator diagnosysManipulator;

	public void setDiagnosysManipulator(DiagnosysManipulator diagnosysManipulator) {
		this.diagnosysManipulator = diagnosysManipulator;
	}

	public void setDoctorManipulator(DoctorManipulator doctorManipulator) {
		this.doctorManipulator = doctorManipulator;
	}

	public DoctorManipulator getDoctorManipulator() {
		return doctorManipulator;
	}

	public void setOrderingManipulator(OrderingManipulator orderingManipulator) {
		this.orderingManipulator = orderingManipulator;
	}

	public OrderingManipulator getOrderingManipulator() {
		return orderingManipulator;
	}

	public void setPersonManipulator(PersonManipulator personManipulator) {
		this.personManipulator = personManipulator;
	}

	public PersonManipulator getPersonManipulator() {
		return personManipulator;
	}

	@RequestMapping("/doctor/doctorHome.htm")
	public ModelAndView handleDoctorHome() {

		return new ModelAndView("doctor/doctorHome");
	}

	@RequestMapping("/doctor/myOrderings.htm")
	public ModelAndView handleManipulateOrderings() {

		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		Doctor doctor = this.doctorManipulator.geByIdPerson(me.getId());

		HashMap<String, Object> myModel = new HashMap<String, Object>();
		String krs = doctor.getDiagnosticlab().getKrsnumber();
		this.orderingManipulator.updateToDiagnosticlabAchive(krs);
		myModel.put("orderings", this.orderingManipulator
				.getAllByDiagnosticlab(krs));

		ModelAndView result = new ModelAndView(
				"doctor/manipulateOrderings", "model", myModel);

		return result;
	}

	@RequestMapping("/doctor/orderingDetails.htm")
	public ModelAndView handleOrderingDetails(
			@RequestParam("orderingDetailsId") long id) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		Ordering ordering = this.orderingManipulator.getById(id);
		myModel.put("ordering", this.orderingManipulator.getById(id));
		myModel.put("pictures", ordering.getPictures());

		ModelAndView result = new ModelAndView("doctor/orderingDetails",
				"model", myModel);

		return result;
	}

	@RequestMapping("/doctor/addDiagnosysHome.htm")
	public ModelAndView handleAddDiagnosysHome(
			@RequestParam("orderingId") long id) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("ordering", this.orderingManipulator.getById(id));

		return new ModelAndView("doctor/addDiagnosysHome",
				"model", myModel);
	}

	@RequestMapping("/doctor/addDiagnosys.htm")
	public ModelAndView handleAddDiagnosys(
			@RequestParam("orderingId") long id,
			@RequestParam("addDiagnosysText") String text,
			@RequestParam("addDiagnosysAdditionalinformations") String additionalinformations) {


		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		Doctor doctor = this.doctorManipulator.geByIdPerson(me.getId());
		Ordering ordering = this.orderingManipulator.getById(id);
		ordering.setStatus("zrealizowane");
		this.orderingManipulator.update(ordering);

		Diagnosys diagnosys = new Diagnosys();
		diagnosys.setText(text);
		diagnosys.setAdditionalinformations(additionalinformations);
		diagnosys.setDate(new Date());

		this.diagnosysManipulator.create(diagnosys, doctor, ordering);

		return handleManipulateOrderings();
	}


	@RequestMapping("/doctor/rejectOrderingHome.htm")
	public ModelAndView handleRejectOrderingHome(
			@RequestParam("orderingId") long id) {

		HashMap<String, Object> myModel = new HashMap<String, Object>();

		myModel.put("ordering", this.orderingManipulator.getById(id));

		return new ModelAndView("doctor/rejectOrderingHome",
				"model", myModel);
	}

	@RequestMapping("/doctor/rejectOrdering.htm")
	public ModelAndView handleRejectOrdering(
			@RequestParam("orderingId") long id,
			@RequestParam("addDiagnosysText") String text,
			@RequestParam("addDiagnosysAdditionalinformations") String additionalinformations) {


		Person me = ((Person) SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal());

		me = personManipulator.getById(me.getId());

		Doctor doctor = this.doctorManipulator.geByIdPerson(me.getId());
		Ordering ordering = this.orderingManipulator.getById(id);
		ordering.setStatus("odrzucone");
		this.orderingManipulator.update(ordering);

		Diagnosys diagnosys = new Diagnosys();
		diagnosys.setText(text);
		diagnosys.setAdditionalinformations(additionalinformations);
		diagnosys.setDate(new Date());

		this.diagnosysManipulator.create(diagnosys, doctor, ordering);

		return handleManipulateOrderings();
	}
}
